#include "StdAfx.h"
#include "TPolyLine.h"
#include "TLine.h"
#include "topengl.h"

void TPolyLine::DrawMySelf(TOpenGL *pGL) const
{
	TOpenGL::SetTexturePlane(m_Normal);
	pGL->GlNormal3d(m_Normal);
	pGL->GluTessBeginContour();
	for (size_t i = 0; i < m_ptList.size(); ++i)
		pGL->GluTessVertex(m_ptList[i]);
	pGL->GluTessEndContour();
}

TExtent TPolyLine::GetExtent() const
{
	TExtent ext;
	for (size_t i = 0; i < m_ptList.size(); ++i)
		ext.Add(m_ptList[i]);
	return ext;
}

TPolyLine& TPolyLine::operator==( const TPolyLine &poly )
{
	if (this == &poly)
		return *this;
	m_ptList = poly.m_ptList;
	m_Normal = poly.m_Normal;
	return *this;
}

void TPolyLine::TransformBy( const AcGeMatrix3d &mat )
{
	for (size_t i = 0; i < m_ptList.size(); ++i)
		m_ptList[i].transformBy(mat);
}

void TPolyLine::CalcNormal( const TArrayPts &lst, AcGeVector3d &v)
{
	if (lst.size() < 3)
		return;

	M3DVector3d vec[4];
	TArrayPts::const_iterator it = lst.begin();
	for (int i = 0; i < 3; ++i, ++it)
	{
		vec[i][0] = it->x;
		vec[i][1] = it->y;
		vec[i][2] = it->z;
	}
	m3dFindNormal(vec[3], vec[0], vec[1], vec[2]);
	m3dNormalizeVector(vec[3]);
	v.x = vec[3][0];
	v.y = vec[3][1];
	v.z = vec[3][2];
}

bool TPolyLine::IsCoplanarTo( const TPolyLine &Poly )
{
	AcGePlane plane1(m_ptList[0], m_ptList[1], m_ptList[2]);
	AcGePlane plane2(Poly.m_ptList[0], Poly.m_ptList[1], Poly.m_ptList[2]);
	return plane1.isCoplanarTo(plane2) == Adesk::kTrue ? true : false;
}